var cmsnodepropertyinfoManager;
var cmsnodepropertyinfoItemPageOpen;
var cmsnodepropertyinfoItem;

function initCmsNodePropertyInfoManager(){
	var colimnsArray = [];
	
		colimnsArray.push({
			display : '属性名',
			name : 'name',
			align : 'left',
			showTitle : true,
			minWidth : 50,
			align : "center",
			editor: { type: 'select', data: propertyKeyData, valueColumnName: 'value' },
			render:function(item){ return defaultRenderFun(item.name,propertyKeyData);},
		});
		colimnsArray.push({
			display : '属性值',
			name : 'val',
			align : 'left',
			showTitle : true,
			minWidth : 50,
			align : "center",
			editor: { type: 'text'},
			isSort : false
		});
		colimnsArray.push({
			display : '节点',
			name : 'nid',
			align : 'left',
			showTitle : true,
			minWidth : 50,
			align : "center",
			render:function(item){
				if(item.cmsNodeInfo){
					return item.cmsNodeInfo.name;
				}else{
					return item.nid;
				}
			},
			isSort : false
		});
	
	var menu = $.ligerMenu({ width: 120, items:
		[
			{ text: '增加', click: function(){openCmsNodePropertyInfoItemPage('create',"新增")}, icon: 'add' },
			{ line: true },
			{ text: '修改', click: function(){openCmsNodePropertyInfoItemPage('edit',"编辑")} },
			{ line: true },
			{ text: '查看', click: function(){openCmsNodePropertyInfoItemPage('show',"查看")} }
		]
	}); 
	
	return window['g'] = jQuery("#maingrid").ligerGrid({
		height : '100%',
		url:'/admin/cms/cmsNodePropertyInfo/ajaxList.do?nid='+cmsNode.id,
		columns : colimnsArray,
		pageSize : 10,
		rownumbers : true,
		toolbar : {
			items : [ {
				text : '保存',
				click : saveUpdateCmsNodePropertyInfoManager,
				icon : 'modify'
			}, {
				line : true
			}, {
				text : '增加',
				click : function(){
					cmsnodepropertyinfoManager.addRow({name:propertyKeyData[0].value,nid:cmsNode.id});
				},
				icon : 'add'
			}, {
				line : true
			},{
				text : '删除',
				click : function(){
					var row = cmsnodepropertyinfoManager.getSelectedRow();
					if (!row.id) { alert('请选择行'); return; }
					jQuery.ligerDialog.confirm('确定要删除吗?',function (yes,value) {
						if(yes){
							jQuery.ajax({
								url:'/admin/cms/cmsNodePropertyInfo/ajaxDelete.do',
								data:{id:row.id},
								dataType:'text',
								type:'post',
								success:function(data){
									if(data == 'success'){
										$.ligerDialog.success('恭喜,已经删除成功!');
										cmsnodepropertyinfoManager.loadServerData({pagesize:10});
									}else{
										tip = $.ligerDialog.tip({ title: '提示信息', content: '网络异常,没有保存成功!' });
									}
								},
								error:function(){
									$.ligerDialog.error("网络发生异常,请稍后再试!");
								}
							});
						}
					});
				},
				icon : 'add'
			}, {
				line : true
			}]
		},
		/*onContextmenu : function (parm,e)
        {
			cmsnodepropertyinfoItem = parm.data;
	        menu.show({ top: e.pageY, left: e.pageX });
	        return false;
        },*/
        enabledEdit: true, 
        isScroll: false, 
        rownumbers:true
	});
};

function saveUpdateCmsNodePropertyInfoManager(){
	var data = cmsnodepropertyinfoManager.getUpdated();
	if(data.length == 0){
		data = cmsnodepropertyinfoManager.getAdded();
		if(data.length == 0){
			$.ligerDialog.warn("您还未做任何修改哦!");
			return;
		}
	}
	doSaveCmsNodePropertyInfos(data);
};
function doSaveCmsNodePropertyInfos(newData,add){
	jQuery.ajax({
		url:'/admin/cms/cmsNodePropertyInfo/saveOrUpdate.do',
		data:{json:JSON.stringify(newData)},
		dataType:'json',
		type:'post',
		success:function(data){
			if(data.sucessList.length > 0){
				$.ligerDialog.success('恭喜,已经保存成功!');
//				tip = $.ligerDialog.tip({ title: '提示信息', content: '恭喜,已经保存'+data.sucessList.length+'条记录!' });
				cmsnodepropertyinfoManager.loadServerData({pagesize:10});
				cmsnodepropertyinfoItemPageOpen.hidden();
			}else{
				if(data.msg){
					tip = $.ligerDialog.tip({ title: '提示信息', content: data.msg });
				}else{
					tip = $.ligerDialog.tip({ title: '提示信息', content: '网络异常,没有保存成功!' });
				}
			}
		},
		error:function(){
			$.ligerDialog.error("网络发生异常,请稍后再试!");
		}
	});
};
/**
 * 
 * @param item
 * @param target
 */
function openCmsNodePropertyInfoItemPage(target,titleName){
	jQuery.ajax({
		url:'/admin/cms/cmsNodePropertyInfo/'+target+'.do',
		data:{id:(cmsnodepropertyinfoItem?cmsnodepropertyinfoItem.id:0)},
		dataType:'text',
		type:'post',
		success:function(data){
			if(data){
				jQuery("#openDiv").html(data);
				cmsnodepropertyinfoItemPageOpen = $.ligerDialog.open({ target: $("#openDiv"),isResize:true,title:titleName,width:450 });
			}
		},
		error:function(){
			$.ligerDialog.error("网络发生异常,请稍后再试!");
		}
	});
};

jQuery(function() {
	cmsnodepropertyinfoManager = initCmsNodePropertyInfoManager();
    jQuery("#pageloading").hide();
});